import {take,call,put,select} from 'redux-saga/effects'
import {get, post} from '../axios'
import {actionsTypes as IndexActionTypes} from '../reducers'
import {actionsTypes as UserActionTypes} from '../reducers/user'
import history from '../history';

export function* change_mobile (mobile, vcode) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/changeMobile`, {mobile, vcode});
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* changeMobileFlow () {
    while (true){
        let req = yield take(UserActionTypes.CHANG_MOBILE);
        let res = yield call(change_mobile, req.mobile, req.vcode);
        if(res){
            if (res.success) {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '绑定成功', msgType: 'success'});
                yield put({type: UserActionTypes.GET_USER_INFO})
                yield call( ()=>setTimeout( function () {
                    history.push('/personal')
                }, 1000))
            } else {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res.msg, msgType: 'fail'});
            }
        }
    }
}

export function* real_name_certification (data) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/checkIdCard`, data);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* realnameCertificationFlow () {
    while (true){
        let req = yield take(UserActionTypes.REAL_NAME_CERTIFICATION);
        let res = yield call(real_name_certification, req.data);
        if(res){
            if (res.success) {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '认证成功', msgType: 'success'});
                yield put({type: UserActionTypes.GET_USER_INFO})
                yield call( ()=>setTimeout( function () {
                    history.push('/personal')
                }, 1000))
            } else {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res.msg, msgType: 'fail'});
            }
        }
    }
}

export function* update_user (data) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/changeBaseInfo`, data);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* updateUserFlow () {
    while (true){
        let req = yield take(UserActionTypes.UPDATE_USER);
        let res = yield call(update_user, req.data);
        if(res){
            if (res.success) {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '操作成功', msgType: 'success'});
                yield put({type: UserActionTypes.GET_USER_INFO})
                yield call( ()=>setTimeout( function () {
                    history.goBack()
                }, 1000))
            } else {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res.msg, msgType: 'fail'});
            }
        }
    }
}

export function* bind_wechat () {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        let openid = yield select(state => state.globalState.openid)
        openid = openid ? openid : localStorage.getItem('openid')
        return yield call(post, `/mem/bindWechat`, {openid});
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* bindWechatFlow () {
    while (true){
        yield take(UserActionTypes.BIND_WECHAT);
        let isWechat = yield select(state => state.globalState.isWechat)
        if (!isWechat) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '当前不在微信环境下', msgType: 'fail'});
        } else {
            let res = yield call(bind_wechat);
            if(res){
                if (res.success) {
                    yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '绑定成功', msgType: 'success'});
                    yield call( ()=>setTimeout( function () {
                        history.goBack()
                    }, 1000))
                } else {
                    let msg = `该微信已被${res.msg.mobile}绑定，请先登录账号解除绑定。`
                    yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: msg, msgType: 'fail'});
                }
            }
        }
    }    
}

export function* bind_invite_code (invite_code) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/bindinviteCode`, {invite_code});
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* bindInviteCodeFlow () {
    while (true){
        let req = yield take(UserActionTypes.BIND_INVITECODE);
        let res = yield call(bind_invite_code, req.invite_code);
        if(res){
            if (res.success) {
                let entry = yield select(state => state.globalState.entry)
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '操作成功', msgType: 'success'});
                yield call( ()=>setTimeout( function () {
                    history.push(entry)
                }, 1000))
            } else {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res.msg, msgType: 'fail'});
            }
        }
    }    
}

export function* unbind_wechat () {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/unBindWechat`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* unbindWechatFlow () {
    while (true){
        yield take(UserActionTypes.UNBIND_WECHAT);
        let res = yield call(unbind_wechat);
        if(res){
            if (res.success) {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '解绑成功', msgType: 'success'});
                yield call( ()=>setTimeout( function () {
                    history.goBack()
                }, 1000))
            } else {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res.msg, msgType: 'fail'});
            }
        }
    }    
}

export function* get_inviter_info (invite_code) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/invite-user/${invite_code}`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* getInviterInfoFlow () {
    while (true){
        let req = yield take(UserActionTypes.GET_INVITER_INFO);
        let res = yield call(get_inviter_info, req.invite_code);
        if(res){
            if (res.success) {
                yield put({type: UserActionTypes.RESPONSE_INVITER_INFO, data: res.model});
            } else {
                yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res.msg, msgType: 'fail'});
            }
        }
    }    
}

function* save_user_role (data) {
    yield put({type: IndexActionTypes.FETCH_START})
    try {
        return yield call(post, `/mem/user-role`, data)
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* saveUserRoleFlow () {
    while (true) {
        let req = yield take(UserActionTypes.SAVE_USER_ROLE),
            res = yield call(save_user_role, req.data)
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '申请成功', msgType: 'success'});
            yield call( ()=>setTimeout( function () {
                history.goBack()
            }, 1000))
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res.msg, msgType: 'fail'});
        }
    }
}